package com.lauor.smpdb;

import com.lauor.smpdb.pool.SmpDbPool;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

public class SmpDataSource extends SmpConfig implements DataSource {
    private final org.slf4j.Logger LOG = LoggerFactory.getLogger(SmpDataSource.class);

    private SmpDbPool dbPool;

    public SmpDataSource() {
        this.dbPool = new SmpDbPool(this);
    }

    public SmpDataSource(SmpConfig smpConfig) {
        smpConfig.copyStateTo(this);
        this.dbPool = new SmpDbPool(this);
    }

    public void initDbPool() throws SQLException {
        this.dbPool.initPool();
    }

    @Override
    public Connection getConnection() throws SQLException {
        try {
            return this.dbPool.borrowConnection();
        } catch (Exception e) {
            LOG.error("getConnection error", e);
            throw new SQLException(e);
        }
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
}